/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-01-05
 * Time: 10:34
 */
class MyStack {
    Queue<Integer>  queue1;
    Queue<Integer>  queue2;


    public MyStack() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();


    }

    public void push(int x) {
        if(!queue1.isEmpty()){
            queue1.offer(x);
        }else if(!queue2.isEmpty()){
            queue2.offer(x);
        }else{
            queue1.offer(x);
        }


    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(!queue1.isEmpty()){
            int size=queue1.size();
            while(size-1!=0){
                int val= queue1.poll();
                size--;
                queue2.offer(val);
            }
            return   queue1.poll();


        }else{
            int  size=queue2.size();
            while(size-1!=0){
                int val= queue2.poll();
                size--;
                queue1.offer(val);


            }
            return     queue2.poll();


        }



    }

    public int top() {
        if(empty()){
            return -1;
        }
        if(!queue1.isEmpty()){
            int val=-1;
            int size=queue1.size();
            while(size!=0){
                val= queue1.poll();
                size--;
                queue2.offer(val);
            }
            return  val;


        }else{
            int  size=queue2.size();
            int val=-1;
            while(size!=0){
                val= queue2.poll();
                size--;
                queue1.offer(val);


            }
            return    val;
        }
    }


    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();



    }
}



/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */
